pipeline {
    agent any
    
    stages {
        stage('Build') {
            steps {
                echo 'Building...'
                sh './gradlew assemble'
            }
        }
        
        stage('Test') {
            steps {
                echo 'Testing...'
                sh './gradlew test'
                
            }
        }
        
        stage('Upload artifacts') {
          steps {
            echo "GIT_COMMIT is ${env.GIT_COMMIT}"
            echo "GIT_BRANCH is ${env.GIT_BRANCH}"

            sh 'tar cvf artifacts.tar build/libs manifest*yml'
            sh 'gzip artifacts.tar'

            script {
              def server = Artifactory.server 'ccp2-artifactory'
              def buildInfo = Artifactory.newBuildInfo()
              buildInfo.env.capture = true

              def build = "build-${env.GIT_BRANCH}-${env.GIT_COMMIT}"

              def uploadSpec = """{
                "files": [{
                  "pattern": "artifacts.tar.gz",
                  "target":  "libs-snapshot/${build}.tar.gz"
                }]
              }"""

          server.upload(uploadSpec, buildInfo)
          server.publishBuildInfo(buildInfo)
        }
      }

        post {
            always {
            cleanWs()
            }
        }
      }
        
        stage('Deploy development release') {
            when {
                branch 'development'
            }
            steps {
                echo 'Deploying development branch....'

                echo "GIT_COMMIT is ${env.GIT_COMMIT}"
                echo "GIT_BRANCH is ${env.GIT_BRANCH}"

                script {
                    def server = Artifactory.server 'ccp2-artifactory'

                    def build = "build-${env.GIT_BRANCH}-${env.GIT_COMMIT}"

                    def downloadSpec = """{
                        "files": [{
                            "pattern": "libs-snapshot/${build}.tar.gz",
                            "target":  "artifacts.tar.gz"
                        }]
                    }"""

                    server.download(downloadSpec)
                }

                sh 'tar xvzf artifacts.tar.gz'

                pushToCloudFoundry(
                    organization: 'CCP2-FS2021',
                    cloudSpace: 'stud-pacegab1',
                    target: 'https://api.cf.cloudlab.zhaw.ch',
                    credentialsId: 'init-cloudlab',
                    manifestChoice: [manifestFile: 'manifest-dev.yml']
                )
            }
        }
        
        stage('Deploy production release') {
            steps {
                echo 'Deploying...'
//                 pushToCloudFoundry(
//                     organization: 'CCP2-FS2021',
//                     cloudSpace: 'stud-pacegab1',
//                     target: 'https://api.cf.cloudlab.zhaw.ch',
//                     credentialsId: 'init-cloudlab',
//                     manifestChoice: [manifestFile: 'manifest.yml']
//                 )
            }
        }
  }
}
